//
//  HXRootViewController.m
//  Number To English
//
//  Created by 朱志雄 on 2024/7/23.
//

#define STATE_BAR_HEIGHT [UIApplication sharedApplication].statusBarFrame.size.height
#define FIT_WIDTH   [UIScreen mainScreen].bounds.size.width/375
#define FIT_HEIGHT  [UIScreen mainScreen].bounds.size.height/667
#define KScreen_W  [UIScreen mainScreen].bounds.size.width
#define KScreen_H  [UIScreen mainScreen].bounds.size.height

#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]
#define ZXMainColor RGBACOLOR(209, 58, 42, 1)
#define BOTTOM_SAFE_HEIHT ((STATE_BAR_HEIGHT > 20)?34:0)
#import "HXRootViewController.h"

#import "HXMineViewController.h"
#import "HXHomeViewController.h"
#import <AnyThinkBanner/AnyThinkBanner.h>
#import <AnyThinkRewardedVideo/AnyThinkRewardedVideo.h>
#import <AnyThinkInterstitial/AnyThinkInterstitial.h>
#import "GIOHomeRegister.h"
#import "BICCg.h"
#import "GIOModityBase.h"

@interface HXRootViewController ()<ATBannerDelegate, ATInterstitialDelegate>

@property(nonatomic, assign) CGSize yyBanner_Size;
@property (nonatomic, assign) BOOL yyBannerTry;
@property (nonatomic, assign) BOOL yyFullscreenTry;

@end

@implementation HXRootViewController



- (void)viewDidLoad {
    [super viewDidLoad];
   
    HXHomeViewController *homeVc = [[HXHomeViewController alloc]init];

    [self addChildVc:homeVc title:@"home" image:@"首页_1" selectedImage:@"首页"];
     
    HXMineViewController *secondVC = [[HXMineViewController alloc]init];
    [self addChildVc:secondVC title:@"setting" image:@"setting_1" selectedImage:@"setting"];
    
//    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreen_W, BOTTOM_SAFE_HEIHT + 49)];
//    backView.backgroundColor = [UIColor whiteColor];
//    [self.tabBar addSubview:backView];
    self.tabBar.backgroundColor = [UIColor whiteColor];
    
    self.yyBannerTry = YES;
    self.yyFullscreenTry = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yyRemoveBannerView) name:@"removeBanner" object:nil];
}

#pragma mark - 添加子控制器
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage{
    // 设置子控制器的文字(可以设置tabBar和navigationBar的文字)
    childVc.title = title;
    
    // 设置子控制器的tabBarItem图片
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    // 禁用图片渲染
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:childVc];

//    //设置item按钮
//    childVc.tabBarItem = [[UITabBarItem alloc]initWithTitle:title image:[[UIImage imageNamed:image]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    //未选中字体颜色
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:RGBACOLOR(235, 245, 255, 1),NSFontAttributeName:[UIFont boldSystemFontOfSize:13]} forState:UIControlStateNormal];
    
    //选中字体颜色
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:RGBACOLOR(7, 50, 25, 30),NSFontAttributeName:[UIFont boldSystemFontOfSize:13]} forState:UIControlStateSelected];
    
    [childVc.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, 0)];
    
    // 添加子控制器
    [self addChildViewController:nav];
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self yyReqFullscrrenAd];
    });
//    [self yyReqBannerAd];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self yyRemoveBannerView];
}

//- (void)yyReqBannerAd {
//    
//    float ratio = 320/50.0;
//    self.yyBanner_Size = CGSizeMake(UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.width/ratio);
//    
//    NSDictionary *extra = @{
//        kATAdLoadingExtraBannerAdSizeKey: [NSValue valueWithCGSize:self.yyBanner_Size]
//    };
//    [[ATAdManager sharedManager] loadADWithPlacementID:kAdBanner extra:extra delegate:self];
//}

- (void)yyReqFullscrrenAd {
    NSDictionary *extra = @{
        kATAdLoadingExtraMediaExtraKey:@"123",
    };
    [[ATAdManager sharedManager] loadADWithPlacementID:kAdInterstitial extra:extra delegate:self];
}

- (void)yyShowBanner:(NSString *)pid {
    if ([[ATAdManager sharedManager] bannerAdReadyForPlacementID:pid]) {
        [self yyRemoveBannerView];
        ATBannerView *bannerView = [[ATAdManager sharedManager] retrieveBannerViewForPlacementID:pid];
        if (bannerView != nil) {
            bannerView.delegate = self;
            bannerView.presentingViewController = self;
            bannerView.tag = 3333;
            CGFloat tabBarHeight = self.tabBar.frame.size.height;
            bannerView.frame = CGRectMake(0, KScreen_H  - tabBarHeight- self.yyBanner_Size.height , self.yyBanner_Size.width, self.yyBanner_Size.height);
            [self.view addSubview:bannerView];
        }
    }
}

- (void)yyRemoveBannerView {
    ATBannerView *bannerView = (ATBannerView *)[self.view viewWithTag:3333];
    if (bannerView) {
        [bannerView destroyBanner];
        [bannerView removeFromSuperview];
        bannerView = nil;
        [self.view layoutIfNeeded];
    }
}

- (void)yyShowFullscrren:(NSString *)placementID {
    
    if ([[ATAdManager sharedManager] interstitialReadyForPlacementID:kAdInterstitial]) {
        UIWindow *mainWindow = [self w_1];
        [[ATAdManager sharedManager] showInterstitialWithPlacementID:kAdInterstitial
                                                    inViewController:mainWindow.rootViewController
                                                            delegate:self];
    } else {
        //        [bice.new bic_e:bic_fullscreen_error remark:[NSString stringWithFormat:@"【Reward not Ready】 placementID=%@", placementID]];
    }
}

- (void)didFinishLoadingADWithPlacementID:(NSString *)pid {
    dispatch_async(dispatch_get_main_queue(), ^{
        if ([pid isEqual:kAdBanner]) {
            [self yyShowBanner:pid];
            
        } else if ([pid isEqual:kAdInterstitial]) {
            [self yyShowFullscrren:pid];
        }
    });
}

- (void)didFailToLoadADWithPlacementID:(NSString*)pid error:(NSError*)error {
    //    dispatch_async(dispatch_get_main_queue(), ^{
    if ([pid isEqual:kAdBanner]) {
//        if (self.yyBannerTry == YES) {
//            self.yyBannerTry = NO;
//            [self yyReqBannerAd];
//        }
//
    } else if ([pid isEqual:kAdInterstitial]) {
        
        if (self.yyFullscreenTry == YES) {
            self.yyFullscreenTry = NO;
            [self yyReqFullscrrenAd];
        } else {
//            [self yyReqBannerAd];
        }
    }
    //    });
}

- (void)bannerView:(ATBannerView *)bannerView failedToAutoRefreshWithPlacementID:(NSString *)placementID error:(NSError *)error {
    dispatch_async(dispatch_get_main_queue(), ^{
    });
}

- (void)bannerView:(ATBannerView*)bannerView didTapCloseButtonWithPlacementID:(NSString*)placementID extra:(NSDictionary*)extra {
    dispatch_async(dispatch_get_main_queue(), ^{
        //        [self yyRemoveBannerView];
    });
}

/// BannerView display results
- (void)bannerView:(ATBannerView *)bannerView didShowAdWithPlacementID:(NSString *)placementID extra:(NSDictionary *)extra {
    
}

/// bannerView click
- (void)bannerView:(ATBannerView *)bannerView didClickWithPlacementID:(NSString *)placementID extra:(NSDictionary *)extra {
    
}

- (void)interstitialDidCloseForPlacementID:(NSString *)placementID extra:(NSDictionary *)extra {
//    dispatch_async(dispatch_get_main_queue(), ^{
//        [self yyReqBannerAd];
//    });
}

- (void)interstitialDidShowForPlacementID:(NSString *)placementID extra:(NSDictionary *)extra {
    dispatch_async(dispatch_get_main_queue(), ^{
    });
}

@end
